package com.xbao.core.util;

import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.Map.Entry;
import java.util.Set;
import java.util.TreeMap;

import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Component;

import com.alibaba.fastjson.JSON;

@Component("md5Encryption")
public class Md5Encryption {
    
    // 全局数组
    private final static String[] strDigits = { "0", "1", "2", "3", "4", "5",
            "6", "7", "8", "9", "a", "b", "c", "d", "e", "f" };

    public Md5Encryption() {
    }

    /**
     * 获取签名
     * @param params
     * @param appSecret
     * @return
     */
    public static String getSign(TreeMap<String, String> params, String token) {
        StringBuilder valueSb = new StringBuilder();
        params.remove("token");
        params.remove("sign");
        // 将参数以参数名的字典升序排序
        Set<Entry<String, String>> entrys = params.entrySet();

        // 遍历排序的字典,并拼接value1+value2......格式
        for (Entry<String, String> entry : entrys) {
        	// 该出要注意，已与客户端确认空格是参与验签的
    		if(StringUtils.isNotEmpty(entry.getValue())) {		
    			valueSb.append(entry.getValue()).append("&");
    		}
        }
        valueSb.append(token);
        String sb = valueSb.toString();
        String sign = GetMD5Code(valueSb.toString());
        String paramStr = JSON.toJSONString(params);
        params.put("_authContent", String.format("{\"p\":%s, \"content\":%s, \"sign\":%s}", paramStr, sb, sign));
        return sign;
    }
    // 返回形式为数字跟字符串
    private static String byteToArrayString(byte bByte) {
        int iRet = bByte;
        // System.out.println("iRet="+iRet);
        if (iRet < 0) {
            iRet += 256;
        }
        int iD1 = iRet / 16;
        int iD2 = iRet % 16;
        return strDigits[iD1] + strDigits[iD2];
    }

    // 返回形式只为数字
    private static String byteToNum(byte bByte) {
        int iRet = bByte;
        System.out.println("iRet1=" + iRet);
        if (iRet < 0) {
            iRet += 256;
        }
        return String.valueOf(iRet);
    }

    // 转换字节数组为16进制字串
    private static String byteToString(byte[] bByte) {
        StringBuffer sBuffer = new StringBuffer();
        for (int i = 0; i < bByte.length; i++) {
            sBuffer.append(byteToArrayString(bByte[i]));
        }
        return sBuffer.toString();
    }

    public static String GetMD5Code(String strObj) {
        String resultString = null;
        try {
            resultString = new String(strObj);
            MessageDigest md = MessageDigest.getInstance("MD5");
            // md.digest() 该函数返回值为存放哈希值结果的byte数组
            resultString = byteToString(md.digest(strObj.getBytes()));
        } catch (NoSuchAlgorithmException ex) {
            ex.printStackTrace();
        }
        return resultString;
    }
    
    public static void main(String[] args) {
		System.out.println(GetMD5Code("13716310527866399034993740imei137163105271231231516872305000735488c21c424140822f774cb1584e1ee3d6cb34b314fa79ad5d1d61419746d1"));
		System.out.println(GetMD5Code("13716310527866399034993740imei137163105271231231516872305000735488c21c424140822f774cb1584e1ee3d6cb34b314fa79ad5d1d61419746d1"));
    }
    
//    public static void main(String[] args) {
//    	TreeMap<String, String> params = new TreeMap<String, String>();
//    	params.put("key1", "key1");
//    	params.put("keya", "keya");
//    	params.put("key2", "key2");
//        // 遍历排序的字典,并拼接value1+value2......格式
//        Set<Entry<String, String>> entrys = params.entrySet();
//        for (Entry<String, String> entry : entrys) {
//            System.out.println(entry.getValue());
//        }
//	}

   /* public static void main(String[] args) {
    	Md5Encryption getMD5 = new Md5Encryption();
        *//*System.out.println(getMD5.GetMD5Code("000234234324000"));
        System.out.println(getMD5.GetMD5Code("00000"));*//*
    }*/
}